package com.ymt.bpm.web.rest.platform.fauth;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PUser;
import com.ymt.bpm.model.PUserExt;
import com.ymt.bpm.model.PUserExtref;
import com.ymt.bpm.service.platform.fauth.FaUserService;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.QueryParam;
import java.util.Map;

/**
 * Created by Johnny on 2017/2/17.
 * For user management
 */
@RestController
@RequestMapping("/api/v1/fa")
public class FaUserResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FaUserService us;

    @RequestMapping(value="/users/all", method= RequestMethod.GET)
    public JSONObject allByTenant(HttpServletRequest req,
                                  @QueryParam("queryParams")String queryParams,
                                  @QueryParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("allByTenant tenantId=" + tenantId);
            log.debug("allByTenant queryParams=" + queryParams);
            log.debug("allByTenant timezoneOffset=" + timezoneOffset);
        }
        return us.allByTenant(tenantId, queryParams, timezoneOffset);
    }

    @RequestMapping(value="/users/listall", method= RequestMethod.GET)
    public JSONObject listAll(HttpServletRequest req,
                              @RequestParam(name="search", required=false)String search,
                              @RequestParam(name="sort", required=false)String sort,
                              @RequestParam(name="order", required=false)String order) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String offset = req.getParameter("offset");
        String limit = req.getParameter("limit");
        if (log.isDebugEnabled()) {
            log.debug("listAll tenantId=" + tenantId);
            log.debug("listAll search=" + search);
            log.debug("listAll sort=" + sort);
            log.debug("listAll order=" + order);
            log.debug("listAll offset=" + offset);
            log.debug("listAll limit=" + limit);
        }
        return us.listAll(tenantId, search, sort, order, Integer.parseInt(offset), Integer.parseInt(limit));
    }

    @RequestMapping(value="/user/saveDetailed", method= RequestMethod.POST)
    public JSONObject saveDetailed(HttpServletRequest req,
                                  PUser user,
                                  PUserExt userExt,
                                  PUserExtref userExtref) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String newPwd = req.getParameter("newPwd");
        if (log.isDebugEnabled()) {
            log.debug("saveDetailed tenantId=" + tenantId);
            log.debug("saveDetailed user.getUserId()=" + user.getUserId());
            log.debug("saveDetailed user.getLoginName()=" + user.getLoginName());
            log.debug("saveDetailed userExt.getUserId()=" + userExt.getUserId());
            log.debug("saveDetailed userExt.getPhone()=" + userExt.getPhone());
        }
        user.setPassword(newPwd);
        return us.saveDetailed(tenantId, loginName, displayName, user, userExt, userExtref);
    }

}
